package com.lanyan.system.controller.api;

import com.lanyan.common.annotation.RepeatSubmit;

import java.math.BigDecimal;

import com.lanyan.common.utils.SecurityUtils;
import com.lanyan.system.service.IProductService;
import io.swagger.annotations.Api;

import java.util.List;
import java.util.Date;

import com.lanyan.common.core.domain.R;
import com.lanyan.common.core.domain.PageR;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import io.swagger.annotations.ApiOperation;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.core.domain.R;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import springfox.documentation.annotations.ApiIgnore;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.domain.Collect;
import com.lanyan.system.domain.vo.CollectVo;
import com.lanyan.system.service.ICollectService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 收藏Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@Api(tags = {"收藏"})
@RequestMapping("/api/collect")
public class ApiCollectController extends BaseController {
    @Autowired
    private ICollectService collectService;
    @Autowired
    private IProductService productService;

    /**
     * 查询收藏列表
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),
    })
    @ApiOperation("查询收藏列表")
    public PageR<Collect> list(@ApiIgnore Collect collect) {
        startPage();
        collect.setAccountId(SecurityUtils.getUserId());
        List<Collect> list = collectService.selectCollectList(collect);
        for (Collect item : list) {
            item.setProductVo(productService.getById(item.getProductId()));
        }
        return PageR.page(list);
    }

    /**
     * 是否收藏
     */
    @GetMapping(value = "/have")
    @ApiOperation("是否收藏")
    public R<Boolean> have(Long productId) {
        Collect collect = collectService.lambdaQuery()
                .eq(CollectVo::getDelFlag, "0")
                .eq(CollectVo::getAccountId, SecurityUtils.getUserId())
                .eq(CollectVo::getProductId, productId)
                .one();
        return R.ok(collect != null);
    }

    /**
     * 新增收藏
     */
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping("save")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "所属商品", dataType = "Long", dataTypeClass = Long.class),
    })
    @ApiOperation("新增收藏")
    public R<Collect> add(@ApiIgnore @RequestBody Collect collect) {
        collect.setAccountId(SecurityUtils.getUserId());
        StringUtils.insertRequired(collect);
        collectService.insertCollect(collect);
        return R.ok(collect);
    }

    /**
     * 删除收藏
     */
    @Log(title = "收藏", businessType = BusinessType.DELETE)
    @GetMapping("/del")
    @ApiOperation("删除收藏")
    public AjaxResult remove(Long productId) {
        collectService.lambdaUpdate()
                .eq(CollectVo::getAccountId, SecurityUtils.getUserId())
                .eq(CollectVo::getProductId, productId)
                .set(CollectVo::getDelFlag, "1")
                .update();
        return success("删除成功");
    }
}
